package com.czie.myapplication;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class Sensor {
    public static final int TEMP_WET_SENSOR = 0x08; //温湿度传感器
    public static final int WET_SENSOR = 0x09; //湿度传感器
    public static final int BODY_SENSOR = 0x0a; //ZIGBEE红外人体检测
    public static final int LIGHT_SENSOR = 0x0b; //ZIGBEE光敏
    public static final int RELAY_SENSOR = 0x0c; //zigbee继电器
    public static final int SMOKE_CONCENTRATION_SENSOR = 13;//zigbee烟雾
    public static final int PRESURE_SENSOR = 0x0e; //ZIGBEE大气压
    public static final int ALTITUDE_SENSOR = 0x0f; //ZIGBEE海拔
    public static final int ZIGBEE_SHAKE_SENSOR = 0x10; //震动
    public static final int NOISE_SENSOR = 0x11; //噪声
    public static final int SOUND_SENSOR = 0x12; //声强
    public static final int ZIGBEE_FIRE_SENSOR = 0x13; //火警
    public static final int RAIN_SENSOR = 0x14; //雨滴
    public static final int CO_SENSOR = 0x15;//C0浓度
    public static final int PM25_SENSOR = 0x16; //PM25值
    public static final int METHANAL_SENSOR = 0x17; //甲醛浓度
    public static final int CO2_SENSOR = 0x18; //CO2浓度
    public static final int GAS_CONCENTRATION_SENSOR = 0x21; //烟雾浓度
    public static final int LIGHT1_RELAY = 0x25; //灯光一开关
    public static final int LIGHT2_REALY = 0x24; // 灯光二开关
    public static final int FAN1_RELAY = 0x26; //风扇1开关 
    public static final int ALARM_RALAY = 0x27; //声光报警器
    public static final int GAS_SENSOR = 0x28; //煤气报警器
    public static final int GLASS_SHAKE_SENSOR = 0x29; //玻璃震荡器
    public static final int SMOKE_SENSOR = 0x2a; //工业烟雾
    public static final int LIGHT_BAR_SENSOR = 0x30; //红外光栅
    public static final int WEIGHT_SENSOR = 0x31; //应变
    public static final int DISPLACEMENT_SENSOR = 0x32; //位移
    public static final int PIPE_PRESURE_SENSOR = 0x33; //液压
    public static final int WIND_SPEED_SENSOR = 0x34; //风速
    public static final int WIN_DIRECTION_SENSOR = 0x35; //风向
    public static final int HIGHT_TEMPERATURE_SENSOR = 0x36; //高温传感器
    public static final int POWER_SENSOR = 0x37; //电表功率
    public static final int POWER_CONSUME_SENSOR = 0x37; //电表电量
    public static final int METER = 0x64; // 电表
    //所有传感器类型定义好

    private String sensorId;
    private int type;
    long lastReceiveDataTime;//最后一次接收到数据的时间，用于检测传感器当前是否在线，超过3分钟没收到数据则认为掉线
    private List<String> datas; //接收到的数据存放在这个数组里
    private List<Long> dataTime; //接收数据的时间
    byte serialPort; //串口号->地址码->接口号
    byte addrCode; //地址码
    byte lineNumber; //接口号
    private String value;

    public String getSensorId() {
        return sensorId;
    }

    public void setSensorId(String sensorId) {
        this.sensorId = sensorId;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public Sensor(String sensorId, int type) {
        this.sensorId = sensorId;
        this.type = type;
        datas = new ArrayList<>();
        dataTime = new ArrayList<>();
    }

    public void addData(String data, Long time) {
        datas.add(data);
        dataTime.add(time);
        lastReceiveDataTime = time;
        if (datas.size() > 10) {
            datas.remove(0);
            dataTime.remove(0);
        }
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public List<String> getDatas() {
        return datas;
    }

    public List<Long> getDataTime() {
        return dataTime;
    }

    public String getLatestValue() {
        if (datas.size() > 0) {
            Date date = new Date();
            //设置仅返回15秒以内的最后一条数据，如果超过15S，则认为传感器不在线
            if (date.getTime() / 1000 - this.lastReceiveDataTime < 15) {
                return datas.get(datas.size() - 1);
            }
        }

        return null;
    }
}
